<?php

App::import('Sanitize');
App::import('Model', 'Account');

class Task extends AppModel {

    var $primaryKey = 'taskID';

    /**
     *
     * @param int $tweetID
     * @return tweet
     * the function takes in tweet ID and calles getTweet function from the tweet model which returns a tweet. 
     */
    function getTweet($tweetID) {
        return ClassRegistry::init('Tweet')->getTweet($tweetID);
    }

    /**
     *
     * @param int $taskID
     * @param array of users $usersIDSQL
     * @author Nada
     * @access private
     * the function takes in the task ID and the list of users, it assigns those users to this task by adding records in the tasks_has_users table. 
     */
    function addUsers($taskID, $usersIDSQL) {
        foreach ($usersIDSQL as $userIDSQL) {
            $userID = $userIDSQL;
            $this->query("insert into tasks_has_users(userID,taskID,permissionLevel) values (" . $userID . "," . $taskID . ", 'assignee')");
            $j++;
        }
        // get the task author
        $taskAuthor = $this->find('all', array('conditions' => array('taskID' => $taskID)));
        $this->query("insert into tasks_has_users(userID,taskID,permissionLevel,status) values (" . $taskAuthor[0]['Task']['sentBy'] . "," . $taskID . ", 'author', 'accepted')");
    }

    /**
     *
     * @param int $userID
     * @param int $accID
     * @return array of tasks
     * @author Nada
     * @access public
     * The function takes in the user ID and the account ID it returns list of tasks for this user on this account, it also checks for the status of the task (will be used in the view for color coding the tasks
     */
    function getTasks($userID, $accID) {
        $tasks = $this->query("Select  * from tasks, tasks_has_users where tasks_has_users.taskID = tasks.taskID AND tasks.accID = " . $accID . " AND tasks_has_users.userID = " . $userID. " order by tasks.taskID desc");
        $j = 0;
        return $tasks;
    }

    /**
     *
     * @param int $id
     * @param int $userID
     * @author Nada
     * @access private
     * the function takes in the task ID and user ID, and updates the task status to rejected in the table tasks_has_users 
     */
    function rejectTask($id, $userID) {
        $this->query("update tasks_has_users set status = 'rejected' where tasks_has_users.userID = " . $userID . " and tasks_has_users.taskID = " . $id);
    }

    /**
     * @author Nada
     * @param int $id task ID
     * the function takes in the task ID, and sets the status field in the database to 1 indicating that the task is completed 
     */
    function completeTask($id) {

        $this->id = $id;
        $request = Array('Task' => Array('status' => 'complete'));
         //$this->query("update tasks_has_users set status = 'done' where tasks_has_users.taskID = " . $id);
        $this->saveField('status','complete');
    }

    /**
     *
     * @param int $id
     * @param int $userID
     * @author Nada
     * @access private
     * the function takes in the task ID and user ID, and updates the task status to accepted in the table tasks_has_users 
     */
    function acceptTask($id, $userID) {
        $this->query("update tasks_has_users set status = 'accepted' where tasks_has_users.taskID = " . $id . " and tasks_has_users.userID = " . $userID);
    }

    /**
     * @author Nada
     * @param int $accID
     * @return Array
     * The function takes in the account ID and returns an array containing its account name. 
     */
    function getAccountName($accID) {
        return $this->query("Select twitterName from accounts where accounts.accID = '" . $accID . "'");
    }

    /**
     * @author Nada
     * @param int $accID
     * @param int $userID
     * @return array of users
     * The function takes in the current userID and the account ID and calls the getUsers function in the User model 
     */
    function getUsers($accID, $userID) {

        return ClassRegistry::init('User')->getUsers($accID, $userID);
    }
    function getDeleted($id)
    {
                return ClassRegistry::init('User')->getDeleted($id);
    }

    /**
     *
     * @param int $accID
     * @param int $userID
     * @return int
     * the function takes in the account ID and the user ID and reutrns his permission level 
     */
    function getPermissionLevel($accID, $userID) {
        
        $permission = $this->query("Select permissionLevel from Accounts_has_users where accounts_has_users.userID =" . $userID . " AND accounts_has_users.accID = " . $accID);
        if($permission != null)
            
        return $permission[0]['Accounts_has_users']['permissionLevel'];
        return -1;
    }

    /**
     * @author Nada
     * @param String $userName
     * @return user
     * the function takes in user name and calls the getUser function in the User Model. 
     */
    function getUserID($userName) {

        return ClassRegistry::init('User')->getUser($userName);
    }

    /**
     * @author Nada
     * @param int $authorID
     * @return user
     * the function takes in the user ID of the tweet author and returns the user 
     */
    function getTweetAuthor($authorID) {
//return $this->query("Select users.username, users.image from users where users.userID = ".$authorID);
        return ClassRegistry::init('User')->getTweetAuthor($authorID);
    }

    /**
     * @author Nada
     * @param int $authorAccID
     * @return account
     * the function takes in the account ID of the tweet author and returns the account 
     */
    public function getMentionAuthor($authorAccID) {
        return ClassRegistry::init('Account')->getMentionAuthor($authorAccID);
    }

    /**
     * @author Nada
     * @param int $userID
     * @return String
     * the function takes in the user ID of the tweet author and returns the user name 
     */
    function getUserName($userID) {
        return ClassRegistry::init('User')->getUserName($userID);
    }

    /**
     * @author Nada
     * @param int $id
     * @return messages
     * the function takes in the task ID and returns all the messages on this task.
     */
    function getMessages($id) {
        return ClassRegistry::init('Message')->getMessages($id);
    }
/**
 * The function returns the user's permission level in a specific account
 * @param int $taskID
 * @param int $userID
 * @return String
 * @author Nada 
 */
    function getUserPermission($taskID, $userID) {
        $userPermission = $this->query("select tasks_has_users.permissionLevel from tasks_has_users where tasks_has_users.taskID = " . $taskID . " and tasks_has_users.userID = " . $userID);
        return $userPermission[0]['tasks_has_users']['permissionLevel'];
    }

    /**
     * The function returns all the users enrolled in the task
     * @param int $taskID
     * @return array 
     * @author Nada
     */
    function getTaskUsers($taskID) {
        return $this->query("Select tasks_has_users.userID, tasks_has_users.permissionLevel, tasks_has_users.status from tasks_has_users, users where users.deleted = 0 AND users.userID = tasks_has_users.userID AND tasks_has_users.taskID = " . $taskID);
    }

    /**
     * The function checks whether a user is assigned in a task or not
     * @param int $taskID
     * @param int $userID
     * @return boolean 
     * @author Nada
     */
    function isAssignee($taskID, $userID) {
        $isAssignee = $this->query("select * from tasks_has_users where tasks_has_users.permissionLevel = 'assignee' AND tasks_has_users.status = 'accepted' AND tasks_has_users.taskID = " . $taskID . " And tasks_has_users.userID = " . $userID);

        if ($isAssignee != null) {

            return true;
        }
        return false;
    }

    /**
     * The function checks whether a user is assignee, author or reassigner in a task
     * @param int $taskID
     * @param int $userID
     * @return boolean 
     * @author Nada
     */
    function isEnrolled($taskID, $userID) {
        $isAssignee = $this->query("select * from tasks_has_users where tasks_has_users.permissionLevel = 'assignee' AND tasks_has_users.taskID = " . $taskID . " And tasks_has_users.userID = " . $userID);

        if ($isAssignee != null) {
            return true;
        }
        return false;
    }

        /**
     * The function checks whether a user is  author  in a task
     * @param int $taskID
     * @param int $userID
     * @return boolean 
     * @author Nada
     */
    function isAuthor($taskID, $userID) {
        $isAuthor = $this->query("select * from tasks_has_users where tasks_has_users.permissionLevel = 'author' AND tasks_has_users.status = 'accepted' AND tasks_has_users.taskID = " . $taskID . " And tasks_has_users.userID = " . $userID);

        if ($isAuthor != null) {
            return true;
        }
        return false;
    }
    /**
     * The function returns the available users to reassign the task to.
     * @param imt $taskID
     * @param int $accID
     * @return array 
     * @author Nada
     */

    function getAvailableUsers($taskID, $accID) {
        return $this->query("SELECT *
FROM users u1, accounts_has_users
WHERE accounts_has_users.userID = u1.userID
AND u1.deleted = 0 
AND accounts_has_users.accID =" . $accID . "
    AND ((accounts_has_users.permissionLevel = 1) OR (accounts_has_users.permissionLevel = 2)  OR (accounts_has_users.permissionLevel = 3))
AND u1.userID NOT 
IN (

SELECT u1.userID
FROM tasks_has_users
WHERE tasks_has_users.taskID = " . $taskID . "
AND tasks_has_users.userID = u1.userID
)");
    }

    /**
     * The function reassigns the tasks to list of users
     * @param int $taskID
     * @param int $userID
     * @param array $users 
     * @author Nada
     */
    function reassign($taskID, $userID, $users) {
        // make the user a reassigner
        $this->query("update tasks_has_users set tasks_has_users.permissionLevel = 'reassigner' where tasks_has_users.taskID = " . $taskID . " AND tasks_has_users.userID = " . $userID);
        // make the users assignees
        foreach ($users as $user) {
            $this->query("insert into tasks_has_users(taskID,userID,permissionLevel,status) values(" . $taskID . "," . $user . ",'assignee','pending')");
        }
    }

}